package com.ldk.base.framework.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.apache.http.util.TextUtils;

public class SystemUtil {

    /**
     * 生成UUID
     *
     * @return
     */
    public static String uuid() {
        java.util.UUID uuid = java.util.UUID.randomUUID();
        String strUUID = uuid.toString().replace("-", "");

        return strUUID;
    }

    /**
     * 生成随机值
     *
     * @param max
     * @return
     */
    public static int generateRandom(int max) {
        if (max < 0) max = 1;
        Random random = new Random();
        int rv = random.nextInt(max);

        return rv;
    }

    /**
     * 生成验证码
     *
     * @param max
     * @return
     */
    public static String generateValidCode(int max) {
        if (max < 4) max = 4;

        String validCode = "";
        for (int i = 0; i < max; i++) {
            validCode += generateRandom(9);
        }
        return validCode;
    }

    /**
     * 生成加密盐
     *
     * @param max
     * @return
     */
    public static String generateSalt(int max) {
        if (max < 4) max = 4;

        String validCode = "";
        for (int i = 0; i < max; i++) {
            validCode += generateRandom(9);
        }
        return validCode;
    }

    /**
     * 生成序列号
     *
     * @param prefix
     * @return prefix+yyyyMMddHHmmss+4位随机数
     */
    public static String generateSn(String prefix) {
        Date date = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String sn = prefix + sdf.format(date);

        Random random = new Random();
        int rv = random.nextInt(1000);
        String rs = String.format("%04d", rv);
        sn += rs;

        return sn;
    }

    /**
     * 生成订单编号
     *
     * @param prefix
     * @return prefix+yyMMddmmssSSS+2位随机数
     */
    public static String generateOrderNo(String prefix) {
        Date date = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
        String sn = prefix + sdf.format(date);

        Random random = new Random();
        int rv = random.nextInt(99);
        String rs = String.format("%02d", rv);
        sn += rs;

        return sn;
    }

    /**
     * 生成随机号
     *
     * @param prefix
     * @return prefix+yyMMddmmssSSS+4位随机数
     */
    public static String generateRandomNo(String prefix) {
        Date date = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
        String sn = prefix + sdf.format(date);

        Random random = new Random();
        int rv = random.nextInt(9999);
        String rs = String.format("%04d", rv);
        sn += rs;

        return sn;
    }

    /**
     * 获取从1970-01-01到当前时间的总天数
     *
     * @return
     */
    public static int getAllDays() {
        Date date = new Date();
        int allDays = (int) (date.getTime() / 1000 / 3600 / 24);
        return allDays;
    }

    /**
     * 获取从1970-01-01到指定日期的总天数
     *
     * @param date
     * @return
     */
    public static int getAllDays(Date date) {
        int allDays = (int) (date.getTime() / 1000 / 3600 / 24);
        return allDays;
    }

    /**
     * 获取从1970-01-01到当前时间的总小时数
     *
     * @return
     */
    public static int getAllHours() {
        Date date = new Date();
        int allHours = (int) (date.getTime() / 1000 / 3600);
        return allHours;
    }

    /**
     * 获取从1970-01-01到指定日期的总小时数
     *
     * @param date
     * @return
     */
    public static int getAllHours(Date date) {
        int allHours = (int) (date.getTime() / 1000 / 3600);
        return allHours;
    }

    /**
     * 获取从1970-01-01到当前时间的总秒杀
     *
     * @return
     */
    public static long getAllSeconds() {
        Date date = new Date();
        long allSeconds = date.getTime() / 1000;
        return allSeconds;
    }

    /**
     * 获取从1970-01-01到指定日期的总秒数
     *
     * @param date
     * @return
     */
    public static long getAllSeconds(Date date) {
        long allSeconds = date.getTime() / 1000;
        return allSeconds;
    }

    /**
     * 获取从1979-01-01到当前时间的总毫秒杀
     *
     * @return
     */
    public static long getAllMilliseconds() {
        Date date = new Date();
        return date.getTime();
    }

    /**
     * 获取从1970-01-01到指定日期的总秒数
     *
     * @param date
     * @return
     */
    public static long getAllMilliseconds(Date date) {
        return date.getTime();
    }

    /**
     * 获取当前时间的字符串
     *
     * @param format 默认转化格式 yyyy-MM-dd HH:mm:ss
     * @return String
     */
    public static String getCurrrentDate(String format) {
        if (StringUtils.isEmpty(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date();

        return sdf.format(date);
    }


    /**
     * 获取当前时间戳
     *
     * @param format 时间格式
     * @return
     */
    public static long getCurrentTimespan(String format) {
        String strDate = getCurrrentDate(format);

        return stringToSeconds(strDate, format);
    }

    /**
     * 获取时间的字符串
     *
     * @param format 默认转化格式 yyyy-MM-dd HH:mm:ss
     * @return String
     */
    public static String getDateString(Date date, String format) {
        if (StringUtils.isEmpty(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(date);
    }

    /**
     * 获取当前时间的Long类型
     *
     * @return Long
     */
    public static Long getCurDateLong() {
        Date date = new Date();
        String format = "yyMMddHHmmssSS";
        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return Long.parseLong(sdf.format(date));
    }

    /**
     * 总天数转换为日期格式的字符串
     *
     * @param days
     * @param format
     * @return
     */
    public static String daysToDate(int days, String format) {
        long timespan = days * 24 * 3600 * 1000L;

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date(timespan);

        return sdf.format(date);
    }

    /**
     * 总小时数转换为日期格式的字符串
     *
     * @param hours
     * @param format
     * @return
     */
    public static String hoursToDate(int hours, String format) {
        long timespan = hours * 3600 * 1000L;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date(timespan);

        return sdf.format(date);
    }

    /**
     * 总秒数转换为日期格式的字符串
     *
     * @param seconds
     * @param format
     * @return
     */
    public static String secondsToDate(long seconds, String format) {
        long timespan = seconds * 1000L;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date(timespan);

        return sdf.format(date);
    }

    /**
     * 总毫秒数转换为日期格式的字符串
     *
     * @param seconds
     * @param format
     * @return
     */
    public static String millisecondsToDate(long milliseconds, String format) {
        long timespan = milliseconds;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date(timespan);

        return sdf.format(date);
    }

    /**
     * 将日期字符串转换为日期类型
     *
     * @param date
     * @param format
     * @return Date
     */
    public static Date stringToDate(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 将日期字符串转换为秒的时间戳
     *
     * @param date
     * @param format
     * @return long
     */
    public static long stringToSeconds(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date dd = sdf.parse(date);
            return getAllSeconds(dd);
        } catch (Exception e) {

        }
        return 0;
    }

    /**
     * 数字星期转化位字符星期
     *
     * @param dayOfWeek
     * @return
     */
    public static String dayOfWeekToString(int dayOfWeek) {

        switch (dayOfWeek) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
            default:
                return "";
        }
    }

    /**
     * 将java Date 转化为oracle Date类型
     *
     * @param date
     * @return
     */
    public static java.sql.Date javaDateToSqlDate(java.util.Date date) {
        try {
            java.sql.Date sqlDate = new java.sql.Date(date.getTime());

            return sqlDate;

        } catch (Exception e) {

        }
        return new java.sql.Date(new java.util.Date().getTime());
    }

    /**
     * 校验密码
     * 1、长度不小于6位
     * 2、必须以字母开头
     * 3、必须包含特殊字符
     * 4、必须包含数字
     *
     * @param pwd
     * @return
     */
    public static boolean validPwd(String pwd) {
        if (StringUtils.isEmpty(pwd)) {
            return false;
        }
        if (pwd.length() < 6) {
            return false;
        }
        if (pwd.matches("^[a-zA-z](.*)") && pwd.matches("(.*)[-`=\\\\\\[\\];',./~!@#$%^&*()_+|{}:\"<>?]+(.*)") && pwd.matches("(.*)\\d+(.*)")) {
            return true;
        }
        return false;
    }

    /**
     * 将日期data加指定天数days再返回从1970-01-01到指定日期的总秒数
     *
     * @param date 日期
     * @param days 天数
     * @return
     */
    public static long getAddDaysAllSeconds(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        Date dt1 = calendar.getTime();
        long allSeconds = dt1.getTime() / 1000;
        return allSeconds;
    }

    /**
     * 手机号码隐藏中间4位
     *
     * @param phoneNo 日期
     * @return
     */
    public static String hidePhoneNo(String phoneNo) {
        if (TextUtils.isEmpty(phoneNo) || phoneNo.length() != 11) {
            return phoneNo;
        }
        return phoneNo.substring(0, 3) + "****" + phoneNo.substring(7);
    }
    public static String desensitizedName(String fullName){
        if (!StringUtils.isEmpty(fullName)) {
            String name = StringUtils.left(fullName, 1);
            return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
        }
        return fullName;
    }
    //https://blog.csdn.net/weixin_44958006/article/details/110244928
    public static String desensitizedPhoneNumber(String phoneNumber){
        if(StringUtils.isNotEmpty(phoneNumber)){
            phoneNumber = phoneNumber.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
        }
        return phoneNumber;
    }
    public static String emailEncrypt(String email){
        if(StringUtils.isEmpty(email)){
           // log.warn("phoneNumEncrypt: request parameter 'email' is null!");
            return null;
        }
        String newEmail = email.replaceAll("(^\\w)[^@]*(@.*$)", "$1****$2");
        return newEmail;
    }


}
